package mudbconnection;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import mumethod.*;
import muobject.*;

@Component("DBSchoolService")
public class DBSchoolsService implements DBSchools {

	private JdbcTemplate jdbct;
	
	@Autowired
	public DBSchoolsService(DataSource jdbct) {
		super();
		this.jdbct = new JdbcTemplate(jdbct);
	}

	public List<School> getAllSchools() {
		try {
			@SuppressWarnings("unchecked")
			List<School> schools = (List<School>) jdbct.query(
					"Select * from Schools ", new SchoolMapper());
			return schools;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}
	
	public School getSchoolBySno(String sno) {
		try {
			@SuppressWarnings("unchecked")
			School school = (School) jdbct.queryForObject(
					"Select * from Schools where Sno like ?",new Object[]{ sno }, new SchoolMapper());
			return school;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public School getSpicalSnoSchools(String sno) {
		School school = (School) jdbct.queryForObject(
				"Select * from Schools where sid=?", new Object[] { sno },
				new SchoolMapper());
		return school;
	}

	public void setSpicalSidSchools(School school) {
		//school = base64Object.Base64SchoolEncode(school);
		jdbct.update("update Schools set Sno=?, Cname=?, Ename=?, Publictype=?, Grade=?, " +
				"Daytype=?, Tel=?, Fax=?, Address=?, Tname=?, Ttel=?, Temail=?, " +
				"President=?, Registerdate=?, Updatedate=? where scid=? ", new Object[]{school.getSno(), 
				school.getCname(), school.getEname(),
				school.getPublictype(), school.getGrade(), school.getDaytype(), 
				school.getTel(), school.getFax(), school.getAddress(), 
				school.getTname(), school.getTtel(), school.getTemail(),
				school.getPresident(), school.getRegisterdate(), 
				school.getUpdatedate(), school.getScid()});
		//school = base64Object.Base64SchoolDecode(school);
	}

	class SchoolMapper implements RowMapper {
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			School school = null;
			try {
				school = new School(rs.getInt("Scid"), rs.getString("Sno"),
						rs.getString("Cname"), rs.getString("Ename"),
						rs.getString("Publictype"), rs.getString("Grade"),
						rs.getString("Daytype"), rs.getString("Tel"),
						rs.getString("Fax"), rs.getString("Address"),
						rs.getString("Tname"), rs.getString("Ttel"),
						rs.getString("Temail"), rs.getString("President"),
						rs.getDate("Registerdate"), rs.getDate("Createdate"),
						rs.getDate("Updatedate"));
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
			//return base64Object.Base64SchoolDecode(school);
			return school;
		}
	}
}
